package com.zcf.service.impl;

import com.zcf.pojo.Dining;
import com.zcf.pojo.Riding;
import com.zcf.pojo.User;
import com.zcf.common.json.Body;
import com.zcf.mapper.LogMapper;
import com.zcf.mapper.RidingMapper;
import com.zcf.mapper.UserMapper;
import com.zcf.service.RidingService;
import com.zcf.utils.Hutool;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zhouzhuang
 * @since 2018-11-28
 */
@Service
public class RidingServiceImpl extends ServiceImpl<RidingMapper, Riding> implements RidingService {
	@Autowired
	private RidingMapper ridingMapper;
	@Autowired
	private UserMapper userMapper;
	@Autowired
	private LogMapper logMapper;

	@Override
	public Body ridShouRu(String ridId) {
		User user = new User();
		user = user.selectById(ridId);
		if (user == null) {
			return Body.newInstance(-1,"用户不存在");
		}
		Double daymoney = 0.0;
		Double zongmoney=0.0;
		daymoney = logMapper.ridShouRuDay(ridId);
		zongmoney = logMapper.ridShouRuZong(ridId);
		Map map = new HashMap<>();
		map.put("day", daymoney);
		map.put("zong", zongmoney);
		return Body.newInstance(map);
	}

	public Body change(String phone, String name, Integer age, String sex, String zheng, String fan, String userId) {
		Riding rs = new Riding();
		EntityWrapper<Riding> ewsq = new EntityWrapper<>();
		ewsq.eq("user_id", userId);
		int count = rs.selectCount(ewsq);
		if (count > 0) {
			return Body.newInstance("你已经是骑手了");
		}
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String time = sdf.format(date);
		Riding riding = new Riding();
		riding.setPhone(phone);
		Riding one = ridingMapper.selectOne(riding);
		if (one != null && !"".equals(one)) {
			return Body.newInstance(501, "正在验证");
		}
		riding.setId(Hutool.getId());
		riding.setPhone(phone);
		riding.setName(name);
		riding.setAge(age);
		riding.setSex(sex);
		if (zheng != null && zheng != "" && zheng.length() > 0) {
			riding.setCardZheng(zheng);
		}
		if (fan != null && fan != "" && fan.length() > 0) {
			riding.setCardFan(fan);
		}
		riding.setTime(time);
		riding.setUserId(userId);
		riding.setStatus(0);
		Integer insert = ridingMapper.insert(riding);
		if (insert > 0) {
			return Body.newInstance(riding);
		}
		return Body.newInstance(501, "上传失败");
	}

	public Body changeStatus(String id) {
		Riding riding = new Riding();
		riding.setId(id);
		Riding ri = ridingMapper.selectById(riding);
		if (ri != null && !"".equals(ri)) {
			User user = new User();
			user.setPhone(ri.getPhone());
			User one = userMapper.selectOne(user);
			if (one != null && !"".equals(one)) {
				one.setStatus(1);
				boolean galg = one.updateById();
				if (galg) {
					Dining dining = new Dining();
					EntityWrapper<Dining> ew = new EntityWrapper<>();
					ew.eq("user_id", one.getId());
					dining = dining.selectOne(ew);
					if (dining != null) {
						dining.deleteById();
					}
				}
				return Body.newInstance(ri);
			}
			return Body.newInstance(501, "请用注册的手机号");

		}
		return Body.newInstance(501, "没有认证");
	}

	@Override
	public Body refuseStatus(String id, String reason) {
		Riding riding = new Riding();
		riding.setId(id);
		Riding ri = ridingMapper.selectById(riding);
		if (ri != null && !"".equals(ri)) {
			riding.setStatus(2);
			riding.setReason(reason);
			Boolean falg = riding.updateById();
			if (falg) {
				return Body.BODY_200;
			}
			return Body.BODY_500;
		}
		return Body.newInstance(501, "没有认证");
	}

}
